package com.cskaoyan.config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.cskaoyan.aliyun.WdAliyunClient;

import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;




@Configuration
@EnableConfigurationProperties(WdAliyunProperties.class)//启用配置属性
public class AliyunConfiguration {
    //为了客户端使用其他容器中的信息，需要将其注册为容器中的组件；比如需要获得配置文件中的信息
    @Autowired
    WdAliyunProperties wdAliyunProperties;

    //将配置类使用构造方法注入
    public AliyunConfiguration(WdAliyunProperties wdAliyunProperties) {
        this.wdAliyunProperties = wdAliyunProperties;
    }


    //将阿里云客户端注册为容器的组件，注册的信息是从配置文件中获取的
    //生成阿里云客户端对象需要尚需信心OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    @Bean
    @Scope("prototype")
    public OSS ossClient() {
        String accessKeyId = wdAliyunProperties.getAccessKeyId();
        String accessKeySecret = wdAliyunProperties.getAccessKeySecret();
        String endpoint = wdAliyunProperties.getOss().getEndPoint();
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }

    @Bean
    public WdAliyunClient wdAliyunClient(){//自动调用配置类来创建客户端对象并注册到容器中
        WdAliyunClient wdAliyunClient = new WdAliyunClient();
        String buckName = wdAliyunProperties.getOss().getBucket();
        String urlPrefix = wdAliyunProperties.getOss().getUrlPrefix();
        //将配置文件中的信息注入到客户端中
        wdAliyunClient.setBucketName(buckName);
        wdAliyunClient.setUrlPrefix(urlPrefix);

        wdAliyunClient.setSignName(wdAliyunProperties.getSms().getSignName());
        wdAliyunClient.setTemplateCode(wdAliyunProperties.getSms().getTemplateCode());

        return wdAliyunClient;
    }

    @Bean
    @Scope("prototype")
    public AsyncClient asyncClient() {
        String accessKeyId = wdAliyunProperties.getAccessKeyId();
        String accessKeySecret = wdAliyunProperties.getAccessKeySecret();

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                //.region("cn-qingdao") // Region ID
                .region(wdAliyunProperties.getSms().getRegion())
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        return client;
    }


}
